package btree;

import java.io.IOException;

import bufmgr.BufMgrException;
import bufmgr.BufferPoolExceededException;
import bufmgr.HashEntryNotFoundException;
import bufmgr.HashOperationException;
import bufmgr.InvalidFrameNumberException;
import bufmgr.PageNotReadException;
import bufmgr.PagePinnedException;
import bufmgr.PageUnpinnedException;
import bufmgr.ReplacerException;
import diskmgr.DiskMgrException;
import diskmgr.Page;

import global.AttrType;
import global.Convert;
import global.PageId;
import global.RID;
import global.SystemDefs;

public class BTLeafPage extends BTSortedPage {

	public int keyType;

	public BTLeafPage(int key) throws ConstructPageException, IOException {
		super(key);
		setType(NodeType.LEAF);
		keyType = key;
	}
	
	public BTLeafPage(Page page, int keyType) throws IOException {
		  super(page, keyType);
		  this.keyType =keyType;
		  setType(NodeType.LEAF);
		 }
	public BTLeafPage(PageId pid, int keyType) throws ConstructPageException, IOException {
		  super(pid, keyType);
		  this.keyType = keyType;
		  setType(NodeType.LEAF);
		 }

	public KeyDataEntry getKey(RID rid) throws KeyNotMatchException,
			NodeNotMatchException, ConvertException, IOException {
		return BT.getEntryFromBytes(getpage(), getSlotOffset(rid.slotNo),
				getSlotLength(rid.slotNo), keyType, NodeType.LEAF);
	}

	public RID insertRecord(KeyClass key, RID dataRid)
			throws InsertRecException, BufferPoolExceededException,
			HashOperationException, ReplacerException,
			HashEntryNotFoundException, InvalidFrameNumberException,
			PagePinnedException, PageUnpinnedException, PageNotReadException,
			BufMgrException, DiskMgrException, IOException,
			ConstructPageException {
		KeyDataEntry kDE = new KeyDataEntry(key, new LeafData(dataRid));
		RID rid = insertRecord(kDE);
		return rid;
	}

	public KeyDataEntry getFirst(RID rid) throws IOException,
			KeyNotMatchException, NodeNotMatchException, ConvertException {
		RID nextRecord = firstRecord();
		rid.slotNo=nextRecord.slotNo;
		rid.pageNo=nextRecord.pageNo;
		KeyDataEntry kDE = getKey(rid);
		return kDE;
	}

	public KeyDataEntry getNext(RID rid) throws IOException,
			KeyNotMatchException, NodeNotMatchException, ConvertException {
		RID nextRecord = nextRecord(rid);
		if(nextRecord==null)
			return null;
		rid.slotNo=nextRecord.slotNo;
		rid.pageNo=nextRecord.pageNo;
		KeyDataEntry kDE = getKey(rid);
		return kDE;
	}

	public KeyDataEntry getCurrent(RID rid) throws KeyNotMatchException,
			NodeNotMatchException, ConvertException, IOException {
		KeyDataEntry kDE = getKey(rid);
		return kDE;
	}

	public boolean delEntry(KeyDataEntry dEntry) throws DeleteRecException,
			KeyNotMatchException, NodeNotMatchException, ConvertException,
			IOException {
		RID rid = new RID();
//		LeafData data = ((LeafData) (dEntry.data));
//		rid = data.getData();
//		return deleteSortedRecord(rid);
		 KeyDataEntry kDE = getFirst(rid);
		 while (kDE!=null) {
		 if(BT.keyCompare(dEntry.key, kDE.key)==0){
		 return deleteSortedRecord(rid);
		 }
		 kDE=getNext(rid);
		 }
		 throw new IOException();
	}

}
